'''
Created on Jan 20, 2010

@author: jcoimbra
'''

import zipfile
import difflib

def newArchive (fileName, archiveName):
  """Creates an empty archive.
  
  fileName: Name of the initial file to store in the archive.
  archiveName: Name of the archive to create.
  """
  archive = zipfile.ZipFile (archiveName, "w")
  archive.write (fileName)
  archive.close ()

def storeDocument (fileName, archiveName):
  #Read the new file into a list of strings
  newFile = open (fileName, "r")
  newStrings = newFile.readlines ()
  #Read the old file into a list of strings
  archive = zipfile.ZipFile (archiveName, "a")
  file = archive.open ("file")
  fileList = file.readlines ()
  oldStrings = []
  for item in fileList:
    oldStrings.append (item.decode ())
  #Make the diff
  diff = difflib.unified_diff (oldStrings, newStrings)
  diffString = ""
  for row in diff:
    diffString = diffString + row
  #Write diff to archive and close it
  archive.writestr ("diff_1", diffString)
  archive.close ()

if __name__ == '__main__':
  storeDocument ("new", "test")
  # newArchive ("file", "test")